importScripts('/node_modules/web-ifc/web-ifc-api.js');

let ifcAPI = new IfcAPI();

onmessage = async ({ data: { path, buffer, id } }) => {
  try {
    await ifcAPI.Init();
    
    if (path === 'load') {
      const modelID = await ifcAPI.OpenModel(buffer, {
        COORDINATE_TO_ORIGIN: true,
        USE_FAST_BOOLS: true
      });
      
      const allItems = [];
      const lines = await ifcAPI.GetAllLines(modelID);
      
      for (let i = 0; i < lines.size(); i++) {
        const line = lines.get(i);
        const itemData = await ifcAPI.GetLine(modelID, line);
        allItems.push(itemData);
      }
      
      postMessage({ id, result: allItems });
      await ifcAPI.CloseModel(modelID);
    }
  } catch (error) {
    postMessage({ id, error: error.toString() });
  }
}; 